import apis from '../../http/apis'
export default {
    namespaced: true,
    state: {
        users: {
            _id: '',
            account: '',
            password: '',
            name: '',
            phone: '',
            salespersonsId: '603c47f7cd0900004d007a82'
        },
        alluser: [],
        id: '',

        pageSize: 10,
        currentPage: 1,
        pages: '',
        total: 0,

        // alltotal:0,
        // adminUser:[],

        value: ''


    },
    mutations: {
        setSalespersonsId(state, id) {
            state.users.salespersonsId = id;
            console.log(state.users.salespersonsId);
        },
        setModify(state, data) {
            state.users = data
        },
        setAdd(state, data) {
            state.users.name = data.name,
                state.users.phone = data.phone,
                state.users.salespersonsId = data.salespersonsId
        },
        setUsers(state, data) {
            // console.log(111,data);
            state.alluser = data
        },
        setUser(state, data) {
            console.log(data);
            state.users = data
        },
        setadminUser(state, data) {
            console.log(data);
            state.adminUser = data
        },
        setUserId(state, _id) {
            state.users._id = _id
        },
        setPage(state, data) {
            state.total = data.total;
            state.currentPage = 1
        },
        setPageSize(state, pageSize) {
            state.pageSize = pageSize;
        },
        setCurrentPage(state, currentPage) {
            state.currentPage = currentPage;
        },
        setTotal(state, total) {
            state.total = total;
        },
        setalltotal(state, total) {
            state.alltotal = total;
        },
        setId(state, id) {
            // state.id = id;
            console.log(state.id);
        },
        setValue(state, value) {
            state.value = value;
        }
    },
    actions: {
        async getUsers(context) {
            const { data } = await apis.users.getUsers({
                id: context.state.id,
                pageSize: context.state.pageSize,
                currentPage: context.state.currentPage,
                value: context.state.value,
                salespersonsId: context.state.users.salespersonsId
            });
            // console.log(data);
            if (data.code) {
                if (JSON.parse(localStorage.loginUser).isAdmin) {
                    context.commit('setUsers', data.data.alluser);
                    context.commit('setTotal', data.data.alltotal);
                } else {
                    context.commit('setUsers', data.data.users);
                    context.commit('setTotal', data.data.total);
                }

            }
        },
        async getUsersById(context) {
            const { data } = await apis.users.getUsersById({
                _id: context.state.users._id,
            });
            // console.log(data);
            if (data.code) {
                context.commit('setModify', data.data[0]);
            }
        },
        async addUsers(context) {
            console.log({
                account: context.state.users.account,
                password: context.state.users.password,
                name: context.state.users.name,
                phone: context.state.users.phone,
                salespersonsId: context.state.users.salespersonsId
            });
            const data = await apis.users.addUsers({
                account: context.state.users.account,
                password: context.state.users.password,
                name: context.state.users.name,
                phone: context.state.users.phone,
                salespersonsId: context.state.users.salespersonsId
            });
            // console.log(data);
            if (data.data[0]) {
                alert('新增成功');
            } else {
                alert('新增失败');
            }
        },
        async updateUsers(context) {
            const data = await apis.users.updateUsers({
                _id: context.state.users._id,
                name: context.state.users.name,
                phone: context.state.users.phone,
            });
            // console.log(data);
        },
        async deleteUsersById(context) {
            const data = await apis.users.deleteUsersById({
                _id: context.state.users._id,
            });
            // console.log(data);
        }
    }
}